import { Node, Vec3 } from 'cc';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { Lineup } from '../level-map/LevelMap.type';
import { Monster } from '../monster/Monster';
import { HomeBaseModelComp } from './model/HomeBaseModelComp';
import { HomeBaseViewComp } from './view/HomeBaseViewComp';

/** 大本营 */
@ecs.register('HomeBase')
export class HomeBase extends ecs.Entity {
  // 数据层
  HomeBaseModel!: HomeBaseModelComp; // 大本营数据

  // 视图层
  HomeBaseView!: HomeBaseViewComp; //
  init() {
    this.addComponents(HomeBaseModelComp);
  }

  /** 加载大本营显示 */
  load(parent: Node, pos: Vec3, generateParent: Node, generatePoint: Vec3, lineup: Lineup) {
    // 1.创建预制件
    const node = ViewUtil.createPrefabNode(this.HomeBaseModel.prefabPath);
    const mv = node.addComponent(HomeBaseViewComp);
    this.add(mv);

    // 2.设置父节点和位置
    node.setParent(parent);
    node.setPosition(pos);

    // 3.怪兽生成点位
    this.HomeBaseModel.generateParent = generateParent;
    this.HomeBaseModel.generatePoint = generatePoint;
    // 4.设置所属阵容
    this.HomeBaseModel.lineup = lineup;
  }

  /**
   * 生成怪兽
   *
   */
  generateMonsters(id: number) {
    const { generateParent, generatePoint, lineup } = this.HomeBaseModel;
    const monster = ecs.getEntity<Monster>(Monster);
    monster.MonsterModel.id = id;
    monster.load(generateParent, generatePoint, lineup);
    this.addChild(monster);
  }

  /** 批量生成怪兽 */
  batchGenerateMonsters(ids: number[]) {}
}
